{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-06-26T01:54:37.651133Z",
     "start_time": "2024-06-26T01:54:37.637712Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "04cf8de8b7c34becbc11b26708c38552\n"
     ]
    }
   ],
   "source": [
    "# azure_search官方demo\n",
    "import os\n",
    "from dotenv import load_dotenv\n",
    "\n",
    "load_dotenv()\n",
    "azure_endpoint = os.getenv('AZURE_OPENAI_API_KEY')\n",
    "print(azure_endpoint)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 写入到向量数据库。\n",
    "\n",
    "import os\n",
    "\n",
    "from langchain_community.vectorstores.azuresearch import AzureSearch\n",
    "from langchain_openai import AzureOpenAIEmbeddings, OpenAIEmbeddings\n",
    "from dotenv import load_dotenv\n",
    "\n",
    "load_dotenv()\n",
    "# Option 2: use an Azure OpenAI account with a deployment of an embedding model\n",
    "\n",
    "# Create embeddings and vector store instances\n",
    "embeddings: AzureOpenAIEmbeddings = AzureOpenAIEmbeddings(\n",
    "    azure_deployment=os.getenv(\"AZURE_DEPLOYMENT_EMBEDDING\"),\n",
    "    openai_api_version=os.getenv(\"AZURE_OPENAI_API_VERSION\"),\n",
    "    azure_endpoint=os.getenv(\"AZURE_OPENAI_ENDPOINT\"),\n",
    "    api_key=os.getenv(\"AZURE_OPENAI_API_KEY\"),\n",
    ")\n",
    "# Create vector store instance\n",
    "index_name: str = \"langchain-vector-demo\"\n",
    "vector_store: AzureSearch = AzureSearch(\n",
    "    azure_search_endpoint=os.getenv(\"AZURE_SEARCH_ENDPOINT\"),\n",
    "    azure_search_key=os.getenv(\"AZURE_SEARCH_KEY\"),\n",
    "    index_name=index_name,\n",
    "    embedding_function=embeddings.embed_query,\n",
    ")\n",
    "\n",
    "# Insert text and embeddings into vector store\n",
    "from langchain_community.document_loaders import TextLoader\n",
    "from langchain_text_splitters import CharacterTextSplitter\n",
    "\n",
    "loader = TextLoader(\"../documents/state_of_the_union.txt\", encoding=\"utf-8\")\n",
    "\n",
    "documents = loader.load()\n",
    "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
    "docs = text_splitter.split_documents(documents)\n",
    "\n",
    "vector_store.add_documents(documents=docs)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "e0adbc96d8ab20fc"
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Groups of citizens blocking tanks with their bodies. Everyone from students to retirees teachers turned soldiers defending their homeland.\n",
      "\n",
      "In this struggle as President Zelenskyy said in his speech to the European Parliament “Light will win over darkness.” The Ukrainian Ambassador to the United States is here tonight.\n",
      "\n",
      "Let each of us here tonight in this Chamber send an unmistakable signal to Ukraine and to the world.\n",
      "\n",
      "Please rise if you are able and show that, Yes, we the United States of America stand with the Ukrainian people.\n",
      "\n",
      "Throughout our history we’ve learned this lesson when dictators do not pay a price for their aggression they cause more chaos.\n",
      "\n",
      "They keep moving.\n",
      "\n",
      "And the costs and the threats to America and the world keep rising.\n",
      "\n",
      "That’s why the NATO Alliance was created to secure peace and stability in Europe after World War 2.\n",
      "\n",
      "The United States is a member along with 29 other nations.\n",
      "\n",
      "It matters. American diplomacy matters. American resolve matters.\n"
     ]
    }
   ],
   "source": [
    "# 读取，从向量数据库。\n",
    "\n",
    "import os\n",
    "from langchain_community.vectorstores.azuresearch import AzureSearch\n",
    "from langchain_openai import AzureOpenAIEmbeddings, OpenAIEmbeddings\n",
    "from dotenv import load_dotenv\n",
    "\n",
    "load_dotenv()\n",
    "# Option 2: use an Azure OpenAI account with a deployment of an embedding model\n",
    "\n",
    "# Create embeddings and vector store instances\n",
    "embeddings: AzureOpenAIEmbeddings = AzureOpenAIEmbeddings(\n",
    "    azure_deployment=os.getenv(\"AZURE_DEPLOYMENT_EMBEDDING\"),\n",
    "    openai_api_version=os.getenv(\"AZURE_OPENAI_API_VERSION\"),\n",
    "    azure_endpoint=os.getenv(\"AZURE_OPENAI_ENDPOINT\"),\n",
    "    api_key=os.getenv(\"AZURE_OPENAI_API_KEY\"),\n",
    ")\n",
    "# Create vector store instance\n",
    "index_name: str = \"langchain-vector-demo\"\n",
    "vector_store: AzureSearch = AzureSearch(\n",
    "    azure_search_endpoint=os.getenv(\"AZURE_SEARCH_ENDPOINT\"),\n",
    "    azure_search_key=os.getenv(\"AZURE_SEARCH_KEY\"),\n",
    "    index_name=index_name,\n",
    "    embedding_function=embeddings.embed_query,\n",
    ")\n",
    "\n",
    "# Perform a similarity search 相似性搜索\n",
    "docs = vector_store.similarity_search(\n",
    "    query=\"what President Zelenskyy said , in his speech to the European Parliament \",\n",
    "    k=3,\n",
    "    search_type=\"similarity\",\n",
    ")\n",
    "print(docs[0].page_content)\n",
    "\n",
    "# Perform a vector similarity search with relevance scores 相关性得分搜索\n",
    "# docs_and_scores = vector_store.similarity_search_with_relevance_scores(\n",
    "#     query=\"what President Zelenskyy said , in his speech to the European Parliament\",\n",
    "#     k=4,\n",
    "#     score_threshold=0.80,\n",
    "# )\n",
    "# from pprint import pprint\n",
    "# \n",
    "# pprint(docs_and_scores)\n",
    "\n",
    "# Perform a hybrid search混合搜索\n",
    "# Perform a hybrid search using the search_type parameter\n",
    "# docs = vector_store.similarity_search(\n",
    "#     query=\"What did the president say about Ketanji Brown Jackson\",\n",
    "#     k=3,\n",
    "#     search_type=\"hybrid\",\n",
    "# )\n",
    "# print(docs[0].page_content)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-06-26T02:01:09.288810Z",
     "start_time": "2024-06-26T02:00:42.458060Z"
    }
   },
   "id": "f0b0ff6b15c87242"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "af9656f9fc612914"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
